package com.dhj.handler;

import com.alibaba.fastjson.JSONObject;
import com.dhj.baseBean.ResponseResult;
import com.dhj.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author DaHuaJia
 * @Description 授权异常处理
 * @Date 2022-04-04 11:24:07
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) {
        // 定义授权异常消息
        ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN.value(), "您的权限不足，禁止访问。");
        // 封装数据到response
        WebUtils.renderString(response, JSONObject.toJSONString(result));
    }

}
